package com.tl.web05.service;

import com.alibaba.fastjson.JSONObject;
import com.tl.web05.config.Config;
import com.tl.web05.constant.CodeEnum;
import com.tl.web05.constant.ResponseResult;
import com.tl.web05.dao.UserDao;
import com.tl.web05.model.entity.User;
import com.tl.web05.utils.StringUtil;

import javax.servlet.ServletContext;
import java.util.HashMap;

public class LoginService {
    private UserDao userDao;

    public LoginService() {
    }

    public LoginService(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 登录(map版)
     *
     * @author Antg
     * @date 2021/7/30 19:50
     */
    public ResponseResult login(HashMap<String, User> map, String username, String password) {
        String msg = "";
        Integer code = 0;
        JSONObject jsonObject = null;
        try {
            //非空校验
            if (StringUtil.notNullVerify(username) || StringUtil.notNullVerify(username)) {
                msg = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getMsg();
                code = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getCode();
            } else {
                //查询map中是否有该用户
                if (map.get(username) == null) {
                    msg = CodeEnum.NOT_REGISTERED.getMsg();
                    code = CodeEnum.NOT_REGISTERED.getCode();
                } else {
                    if (!map.get(username).getPassword().equals(password)) {
                        msg = CodeEnum.WRONG_PASSWORD.getMsg();
                        code = CodeEnum.WRONG_PASSWORD.getCode();
                    } else {
                        msg = CodeEnum.LOGIN_SUCCESSFUL.getMsg();
                        code = CodeEnum.LOGIN_SUCCESSFUL.getCode();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult<Boolean>(msg, code, true);
    }

    /**
     * 登录(数据库版)
     *
     * @author Antg
     * @date 2021/7/30 19:50
     */
    public ResponseResult login(String username, String password) {
        String msg = "";
        Integer code = 0;
        try {
            //非空校验
            if (StringUtil.notNullVerify(username) || StringUtil.notNullVerify(username)) {
                msg = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getMsg();
                code = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getCode();
            } else {
                //查询map中是否有该用户
                if (!userDao.existUsername(username)) {
                    msg = CodeEnum.NOT_REGISTERED.getMsg();
                    code = CodeEnum.NOT_REGISTERED.getCode();
                } else {
                    User user = userDao.getUserByUserNameAndPassword(username, password);
                    if (user == null || !user.getPassword().equals(password)) {
                        msg = CodeEnum.WRONG_PASSWORD.getMsg();
                        code = CodeEnum.WRONG_PASSWORD.getCode();
                    } else {
                        msg = CodeEnum.LOGIN_SUCCESSFUL.getMsg();
                        code = CodeEnum.LOGIN_SUCCESSFUL.getCode();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult<Boolean>(msg, code, true);
    }

    /**
     * 注册(map版)
     *
     * @author Antg
     * @date 2021/7/30 19:51
     */
    public ResponseResult register(ServletContext servletContext, User user) {
        String msg = "";
        Integer code = 0;
        String username = user.getUsername();
        try {
            //获取userMap
            HashMap<String, User> userMap = (HashMap<String, User>) servletContext.getAttribute(Config.USER_MAP);
            if (StringUtil.notNullVerify(username) || StringUtil.notNullVerify(username)) {
                msg = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getMsg();
                code = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getCode();
            } else {
                //判断用户名是否已经存在
                if (userMap.get(username) != null) {
                    msg = CodeEnum.USER_ALREADY_EXISTS.getMsg();
                    code = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getCode();
                } else {
                    userMap.put(username, user);
                    msg = CodeEnum.REGISTRATION_SUCCESS.getMsg();
                    code = CodeEnum.REGISTRATION_SUCCESS.getCode();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult<Boolean>(msg, code, true);
    }

    /**
     * 注册(数据库版)
     *
     * @author Antg
     * @date 2021/7/30 19:42
     */
    public ResponseResult register(User user) {
        String msg = "";
        Integer code = 0;
        String username = user.getUsername();
        try {
            if (StringUtil.notNullVerify(username) || StringUtil.notNullVerify(username)) {
                msg = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getMsg();
                code = CodeEnum.USERNAME_OR_PASSWORD_IS_EMPTY.getCode();
            } else {
                //判断用户名是否已经存在
                if (userDao.existUsername(username)) {
                    msg = CodeEnum.USER_ALREADY_EXISTS.getMsg();
                    code = CodeEnum.USER_ALREADY_EXISTS.getCode();
                } else {
                    userDao.add(user);
                    msg = CodeEnum.REGISTRATION_SUCCESS.getMsg();
                    code = CodeEnum.REGISTRATION_SUCCESS.getCode();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseResult<Boolean>(msg, code, true);
    }
}
